package com.liu.system.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.google.common.collect.Maps;
import com.liu.common.domain.ResultVo;

import com.liu.common.utils.JacksonUtil;
import com.liu.system.manage.domain.DUser;
import com.liu.system.manage.service.IMenuService;
import com.liu.system.manage.service.IUserService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;



import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RefreshScope
@RequestMapping("/vue-admin-template/user")
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IMenuService menuService;




    @PostMapping("/login")
    public ResultVo login(String username,String password){
        log.info("name:"+username +"password" +password);

        Map map = Maps.newHashMap();
        map.put("token","my-admin-token");

      return ResultVo.success(map);
    }

    @PostMapping("/logout")
    public ResultVo logout(){


        Map map = Maps.newHashMap();
        map.put("token","my-admin-token");

        return ResultVo.success();
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public ResultVo info(){
        Map map = Maps.newHashMap();
        List roles = new ArrayList<>();
        roles.add("admin");
        map.put("roles",roles);
        map.put("introduction","我是超人我会飞");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","天生小菜");


        return  ResultVo.success(map);
    }

    /**
     * 获取分页查询用户列表
     * @param query
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/list")
    public ResultVo list(String query,int page,int limit){
        Map map = Maps.newHashMap();
        IPage<DUser> pages = userService.queryUserForPage(page,limit,query);
        map.put("total",pages.getTotal());
        map.put("items",pages.getRecords());
        return  ResultVo.success(map);
    }

    /**
     *添加用户
     *
     */
    @PostMapping
    public ResultVo add(@RequestBody DUser user){
        log.info("这里是新增用户");
        log.info("user:"+ JacksonUtil.printJson(user));


          userService.insert(user);
        return  ResultVo.success("用户添加成功！！");
    }

    /**
     *更新用户
     *
     */
    @PutMapping
    public ResultVo update(@RequestBody DUser user){
        log.info("这里是更新用户");
        log.info("user:"+ JacksonUtil.printJson(user));


        userService.update(user);
        return  ResultVo.success("用户更新成功！！");
    }


    /**
     *删除用户
     *
     */
    @DeleteMapping
    public ResultVo delete( String id){


        log.info("这里是删除用户"+JacksonUtil.printJson(id));
        userService.delete(id);
        return  ResultVo.success("删除用户成功！！");
    }

    /**
     * 通过用户id 获取当前用户的菜单
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public ResultVo getUserMenuList(@PathVariable("userId") String userId){
        log.info("userId="+userId);
          Map map = new HashMap();



           map.put("btnList",new ArrayList<>());
           map.put("menuList",menuService.getMenuTree("",userId));
//           todo 后续完成按钮权限控制，这里暂时使用空数组进行过标识


        return ResultVo.success(map);

    }




}
